package com.sunbird.crm.base.manager.factory;

/**
 * @ClassName AsynManager
 * @Descriptiom TODO
 * @Author Sunbird
 * @Date 2019/9/7 22:43
 * @Version 1.0
 **/

import com.sunbird.crm.base.utils.Threads;
import com.sunbird.crm.base.utils.spring.SpringUtils;

import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
        /* 异步任务管理器
        *
        * @author liuhulu
        */
public class AsynManager
{
    /**
     * 操作延迟10毫秒
     */
    private final int OPERATE_DELAY_TIME = 10;

    /**
     * 异步操作任务调度线程池
     */
    private ScheduledExecutorService executor = SpringUtils.getBean("scheduledExecutorService");

    /**
     * 单例模式
     */
    private AsynManager(){}

    private static AsynManager me = new AsynManager();

    public static AsynManager me()
    {
        return me;
    }

    /**
     * 执行任务
     *
     * @param task 任务
     */
    public void execute(TimerTask task)
    {
        executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
    }

    /**
     * 停止任务线程池
     */
    public void shutdown()
    {
        Threads.shutdownAndAwaitTermination(executor);
    }
}

